﻿using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using ZoneLinePacking.Common;
using ZoneLinePacking.Core.Utils;
using ZoneLinePacking.Models.Entity.User;
using ZoneLinePacking.Services.DB;
using ZoneLinePacking.Services.Message;
using ZoneLinePacking.ViewModelBase;

namespace ZoneLinePacking.ViewModels.Dialogs {
    public class LoginDialogViewModel : DialogViewModel {

        public LoginDialogViewModel(
            IRegionManager regionManager,
            IDialogService dialog,
            IEventAggregator eventAggregator,
            IMessageService message,
            IDbOperate<IFreeSqlMainContext> mainDbOperate)
            : base(regionManager, dialog, eventAggregator, message, mainDbOperate) {

            AccountList.Clear();
            AccountList.AddRange(new List<string> { "SuperAdmin", "PackingOperator" });

            this.LoginName = AccountList[1];
#if DEBUG
            this.LoginName = AccountList[0];
            this.Password = "ksroot0987";
#endif

        }


        #region Props

        private ObservableCollection<string> _accountList = new ObservableCollection<string>();

        public ObservableCollection<string> AccountList {
            get { return _accountList; }
            set { _accountList = value; RaisePropertyChanged(); }
        }

        public string GrowlToken => nameof(LoginDialogViewModel);

        private string _loginName;
        // <summary>
        /// 登录名
        /// </summary>
        public string LoginName {
            get { return _loginName; }
            set { _loginName = value; RaisePropertyChanged(); }
        }

        private string _password;
        /// <summary>
        /// 密码
        /// </summary>
        public string Password {
            get { return _password; }
            set { _password = value; RaisePropertyChanged(); }
        }

        private bool _isOpen;

        public bool IsOpen {
            get { return _isOpen; }
            set { _isOpen = value; RaisePropertyChanged(); }
        }

        #endregion

        #region Commands
        /// <summary>
        /// 登录命令
        /// </summary>
        public DelegateCommand LoginCommand => new DelegateCommand(() => {
            if (!IsVerify())
                return;

            string passwordTemp = Md5CryptionUtil.EnCryption(Password);

            var user = _mainDbOperate.GetModel<UserEntity>(p => p.Account == LoginName && p.PassWord == passwordTemp);
            if (user == null) {
                PrismIocInfo.MessageService.GrowError($"登录失败！{Environment.NewLine}用户名不存在或者密码错误", GrowlToken);
                return;
            }
            if (user.Status == false) {
                PrismIocInfo.MessageService.GrowError($"登录失败！{Environment.NewLine}该用户登录身份已失效,请联系系统管理员！", GrowlToken);
                return;
            }
            var res =_mainDbOperate.UpdateModel<UserEntity>(o => o.Account == LoginName && o.PassWord == passwordTemp, o => new UserEntity { LastLoginTime = DateTime.Now });
            AppSession.CurrentUser = user;
            base.OnDialogClosed(ButtonResult.OK);
        });

        public DelegateCommand CloseCommand => new DelegateCommand(() => {
            base.OnDialogClosed(ButtonResult.Cancel);
        });
        #endregion

        #region Methods
        //验证
        private bool IsVerify() {
            if (string.IsNullOrEmpty(LoginName)) {
                PrismIocInfo.MessageService.GrowWarning("登录名不能为空！", GrowlToken);
                return default;
            }
            if (string.IsNullOrEmpty(Password)) {
                PrismIocInfo.MessageService.GrowWarning("密码不能为空！", GrowlToken);
                return default;
            }
            return true;
        }
        #endregion
    }
}
